﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件名：DictionariesManage.cs
  //文件功能描述：词典管理
  //创建人：赵国伟
  //生成时间：2012/03/17 16:15:00
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Shopping.Common;
using Shopping.Model;
using Shopping.DAL;

namespace Shopping.BLL
{
    public class DictionariesManage
    {
        private readonly DALDictionaries dalDictionary = new DALDictionaries();

        #region "查询指定类别的词典信息； by 赵国伟 2012-03-18"
        /// <summary>
        /// 查询指定类别的词典信息；
        /// </summary>
        /// <param name="categoryEnum"></param>
        /// <param name="IsAvailable"></param>
        /// <returns></returns>
        public List<Dictionaries> QueryList(DictionaryCategory categoryEnum,bool IsAvailable)
        {
            string cond = "Category='" + categoryEnum.ToString() + "' ";
            if (IsAvailable)
                cond += " and Status='" + Status.正用.ToString() + "'";
            else
                cond += " and Status<>'" + Status.已删除.ToString() + "'";
            string orderby = "OrderValue,DictionaryID desc";
            return dalDictionary.QueryList(cond, orderby);
        }
        #endregion

        #region "查询指定类别的词典信息；并存为Hash by 赵国伟 2012-03-18"
        /// <summary>
        /// 查询指定类别的词典信息；并存为Hash
        /// </summary>
        /// <param name="categoryEnum"></param>
        /// <returns></returns>
        public static Dictionary<int, string> GetDictionariesToHash(DictionaryCategory categoryEnum)
        {
            DictionariesManage bllDictionary = new DictionariesManage();
            List<Dictionaries> data = bllDictionary.QueryList(categoryEnum, false);
            Dictionary<int,string> dict = new Dictionary<int,string>();
            if (data != null)
            {
                foreach (Dictionaries datainfo in data)
                    dict.Add(datainfo.DictionaryID, datainfo.DictionaryName);
            }
            return dict;
        }
        #endregion

        #region "将指定的词典数据生成下拉选项字符串； by 赵国伟 2012-03-18"
        /// <summary>
        /// 将指定的词典数据生成下拉选项字符串；
        /// </summary>
        /// <param name="DictionaryCategory"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string GetDictionariesToddlObject(DictionaryCategory categoryEnum, bool IsAvailable, string defaultValue)
        {
            DictionariesManage bllDictionary = new DictionariesManage();
            StringBuilder sb = new StringBuilder(500);
            List<Dictionaries> data = bllDictionary.QueryList(categoryEnum, IsAvailable);
            if (data != null)
            {
                foreach (Dictionaries datainfo in data)
                {
                    sb.Append("<option value=\"");
                    sb.Append(datainfo.DictionaryID);
                    sb.Append("\" ");
                    if (defaultValue == datainfo.DictionaryID.ToString())
                        sb.Append(" selected ");
                    sb.Append(">");
                    sb.Append(datainfo.DictionaryName);
                    sb.Append("</option>");
                }
            }
            return sb.ToString();
        }
        
        #endregion

        #region "查询指定类别下的词典信息；并输出为复选框选项；  by 赵国伟 2012-03-18"
        /// <summary>
        /// 查询指定类别下的词典信息；并输出为复选框选项；
        /// </summary>
        /// <param name="categoryEnum"></param>
        /// <param name="chkObjectName"></param>
        /// <param name="defaultValue"></param>
        /// <param name="maxLineNumber"></param>
        /// <returns></returns>
        public static string GetDictionariesToCheckOption(DictionaryCategory categoryEnum, string chkObjectName, string defaultValue, int maxLineNumber)
        {
            string cond = "Category='"+categoryEnum.ToString()+"' ";
            string orderby = "OrderValue, CreateDate desc";
            List<IDNameObj> listOfData = DALHelper.QueryIDNameObjs<Dictionaries>(cond,orderby,DictionariesEnum.DictionaryName);
            return BLLHelper.GenerateToCheckOption(listOfData, chkObjectName, defaultValue, maxLineNumber);
        }
        #endregion


        #region "保存词典信息 by 赵国伟 2012-04-04"
        /// <summary>
        /// 保存词典信息
        /// </summary>
        /// <param name="place"></param>
        /// <returns></returns>
        public ReturnValue Save(Dictionaries d)
        {
            ReturnValue ret = new ReturnValue(ResultType.Fail, "保存失败");
            //检查词典名称是否重复；
            if (dalDictionary.QueryList(1, "DictionaryName='" + d.DictionaryName + "' and Category='" + d.Category + "' and DictionaryID<>" + d.DictionaryID, "") != null)
            {
                ret.Message = "词典名称重复！";
                return ret;
            }
            int result = 0;
            if (d.DictionaryID <= 0)
            {
                d.CreateDate = DateTime.Now;
                result = dalDictionary.Add(d);
            }
            else
            {
                Dictionaries o = dalDictionary.GetModel(d.DictionaryID);
                d.CreateDate = o.CreateDate;
                result = dalDictionary.Update(d);
            }
            if (result > 0)
            {
                ret.Message = "保存成功！";
                ret.Result = ResultType.Success;
            }
            return ret;
        }
        #endregion

        #region "查询一条词典信息； by 赵国伟 2012-04-04"
        /// <summary>
        /// 查询一条词典信息；
        /// </summary>
        /// <param name="DictionaryID"></param>
        /// <returns></returns>
        public Dictionaries GetDictionaryInfo(int DictionaryID)
        {
            return dalDictionary.GetModel(DictionaryID);
        }
        #endregion

        #region "删除指定的词典信息； by 赵国伟 2012-04-04"
        /// <summary>
        /// 删除指定的词典信息；
        /// </summary>
        /// <param name="DictionaryID"></param>
        /// <returns></returns>
        public ReturnValue Delete(int DictionaryID)
        {
            ReturnValue ret = new ReturnValue(ResultType.Fail, "删除失败！");
            try
            {
                if (dalDictionary.Delete(DictionaryID) > 0)
                {
                    ret.Result = ResultType.Success;
                    ret.Message = "删除成功！";
                }
            }
            catch
            {
                ret.Message = "删除失败:才词典已被其它数据使用中，请设置为停用，或直接挑战词典名称!";
            }
            return ret;

        }
        #endregion

        #region "分页查询词典表信息； by 赵国伟 2012-04-04"
        /// <summary>
        /// 分页查询词典表信息；
        /// </summary>
        /// <param name="category"></param>
        /// <param name="status"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public List<Dictionaries> QueryListAsPage(string category, string status, AsPage p)
        {
            string cond = "1=1 ";
            if (!string.IsNullOrEmpty(category))
                cond += " and Category='" + category + "'";
            if (!string.IsNullOrEmpty(status))
                cond += " and status='" + status + "'";
            return dalDictionary.QueryListAsPage(cond, "OrderValue,DictionaryID desc", p.CurPage, p.PageSize, out p.TotalCount);
        }
        #endregion

    }
}
